package com.ad.pet.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ad.pet.R;


/**
 * 通用输入布局
 */

public class InputView extends RelativeLayout {
    private RelativeLayout mRelativeLayout;
    private RelativeLayout mRelayoutLeft;
    private TextView mTvTitle;
    private ImageView mIvTitle;
    private RelativeLayout mRlRight;
    private ImageView mIvRight;
    private ImageView mImageClear;
    private TextView mTvRight;
    private View mViewLeft;
    private View mViewLine;
    private EditText mEtContent;
    private LinearLayout mLinearRight;
    private OnContentChanger mOnContentChanger;
    private OnRightClickListener mOnRightListener;
    private OnLeftClickListener onLeftClickListener;
    private boolean mShowClear = true;

    private void assignViews() {
        mRelativeLayout = findViewById(R.id.relativeLayout);
        mRelayoutLeft = findViewById(R.id.relayout_left);
        mTvTitle = findViewById(R.id.tv_title);
        mIvTitle = findViewById(R.id.iv_title);
        mRlRight = findViewById(R.id.rl_right);
        mIvRight = findViewById(R.id.iv_right);
        mImageClear = findViewById(R.id.image_clear);
        mTvRight = findViewById(R.id.tv_right);
        mViewLeft = findViewById(R.id.view_left);
        mViewLine = findViewById(R.id.view_line);
        mEtContent = findViewById(R.id.et_content);
        mLinearRight = findViewById(R.id.linearRight);
    }

    private void initView(Context context) {
        View.inflate(context, R.layout.layout_input_psd, this);
        assignViews();
        initListener();
    }

    public InputView(Context context) {
        this(context, null);
    }

    public InputView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        if (attrs != null) {
            TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.InputView, defStyleAttr, 0);
            //是否显示清空按钮
            mShowClear = a.getBoolean(R.styleable.InputView_IVShowClear, true);
            boolean showContent = a.getBoolean(R.styleable.InputView_IVShowContent, true);//是否显示文本内容
            boolean focus = a.getBoolean(R.styleable.InputView_IVFocus, true);//是否能获取焦点
            int maxLength = a.getInt(R.styleable.InputView_IVMaxLength, 50);
            int leftImage = a.getResourceId(R.styleable.InputView_IVLeftImage, -1);//左边图
            int rightImage = a.getResourceId(R.styleable.InputView_IVRightImage, -1);//右边图片
            int inputType = a.getInt(R.styleable.InputView_IVInputType, InputType.TYPE_CLASS_TEXT);//输入类型

            String leftText = a.getString(R.styleable.InputView_IVLeftText);//左边文字
            String rightText = a.getString(R.styleable.InputView_IVRightText);//右边文字
            String content = a.getString(R.styleable.InputView_IVContent);//输入框内容
            String hintContent = a.getString(R.styleable.InputView_IVHintContent);//输入框提示内容
            boolean isShowLeftLine = a.getBoolean(R.styleable.InputView_IVShowLeftLine, true);
            a.recycle();
            mTvTitle.setText(leftText);
            mEtContent.setHint(hintContent);
            mEtContent.setText(content);
            mTvRight.setText(rightText);

            mEtContent.setInputType(inputType);
            if (inputType == 128) {
                mEtContent.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            setRightResource(rightImage);
            setLeftImfResource(leftImage);
            mEtContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
            mEtContent.setFocusable(focus);
            mEtContent.setClickable(focus);

            //是否显示左侧
            if (leftImage == -1 && TextUtils.isEmpty(leftText)) {
                mRelayoutLeft.setVisibility(GONE);
            } else {
                mRelayoutLeft.setVisibility(VISIBLE);
            }
            if (TextUtils.isEmpty(leftText)) {
                mTvTitle.setVisibility(GONE);
            } else {
                mTvTitle.setVisibility(VISIBLE);
            }
            //是否显示右侧文字
            if (!TextUtils.isEmpty(rightText)) {
                mLinearRight.setVisibility(VISIBLE);
            } else {
                mLinearRight.setVisibility(GONE);
            }
            //是否显示右侧
            if (rightImage == -1 && TextUtils.isEmpty(rightText)) {
                mRlRight.setVisibility(GONE);
            } else {
                mRlRight.setVisibility(VISIBLE);
            }
            if (isShowLeftLine) {
                mViewLeft.setVisibility(VISIBLE);
            } else {
                mViewLeft.setVisibility(GONE);
            }

        }
    }

    /**
     * 设置右边的图片
     *
     * @param resId
     */
    public void setRightResource(int resId) {
        if (resId == -1) {
            mIvRight.setVisibility(GONE);
            return;
        }
        mIvRight.setVisibility(VISIBLE);
        mIvRight.setImageResource(resId);
    }

    /**
     * 设置左边的图标
     *
     * @param resId
     */
    public void setLeftImfResource(int resId) {
        if (resId == -1) {
            mIvTitle.setVisibility(GONE);
            return;
        }
        mIvTitle.setVisibility(VISIBLE);
        mIvTitle.setImageResource(resId);
    }

    private void initListener() {
        mEtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (mOnContentChanger != null)
                    mOnContentChanger.onChange(editable.toString());
                if (editable.length() > 0 && mShowClear) {
                    mImageClear.setVisibility(VISIBLE);
                } else {
                    mImageClear.setVisibility(GONE);
                }
            }
        });
        mImageClear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mEtContent.setText("");
            }
        });
        mRlRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnRightListener != null)
                    mOnRightListener.onRightClick();
            }
        });
        mRelayoutLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onLeftClickListener != null) {
                    onLeftClickListener.onLeftClick();
                }
            }
        });

    }

    public interface OnContentChanger {
        void onChange(String edt);
    }

    public interface OnRightClickListener {
        void onRightClick();
    }

    public interface OnLeftClickListener {
        void onLeftClick();
    }

    /**
     * 获得输入的内容
     *
     * @return
     */
    public String getContent() {
        return TextUtils.isEmpty(mEtContent.getText().toString().trim()) ? "" : mEtContent.getText().toString().trim();
    }

    /**
     * 设置输入的内容
     *
     * @param content
     */
    public void setContent(String content) {
        mEtContent.setText(content);
    }

    /**
     * 设置右边文字的内容---如用于短信验证码
     *
     * @param content
     */
    public void setRightContent(String content) {
        mTvRight.setText(content);
    }

    /**
     * 设置左边选择的内容
     *
     * @param content
     */
    public void setLeftContent(String content) {
        mTvTitle.setText(content);
    }

    /**
     * 设置右边文字的颜色
     *
     * @param color
     */
    public void setRightTextColor(int color) {
        mTvRight.setTextColor(getResources().getColor(color));
    }

    /**
     * 设置右边图片显影
     *
     * @param flag
     */
    private void setRightImgVisiable(boolean flag) {
        if (flag) {
            mIvRight.setVisibility(VISIBLE);
        } else {
            mIvRight.setVisibility(GONE);
        }
    }

    /**
     * 设置内容变化的监听
     *
     * @param mOnContentChanger
     */
    public void setmOnContentChanger(OnContentChanger mOnContentChanger) {
        if (mOnContentChanger != null)
            this.mOnContentChanger = mOnContentChanger;
    }

    public TextView getTvRight() {
        return mTvRight;
    }

    public EditText getEtContent() {
        return mEtContent;
    }

    /**
     * 右边文字点击事件
     *
     * @param mOnRightListener
     */
    public void setOnRightListens(OnRightClickListener mOnRightListener) {
        if (mOnRightListener != null)
            this.mOnRightListener = mOnRightListener;
    }

    public void setOnLeftClickListener(OnLeftClickListener onLeftClickListener) {
        if (onLeftClickListener != null)
            this.onLeftClickListener = onLeftClickListener;
    }
}
